/**
 * @author Ahammer
 * @date   2009 11
 * @update 2011 01 added 16 bit support
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.renderable.ParameterBlock;
import java.text.DecimalFormat;

import javax.media.jai.Histogram;
import javax.media.jai.JAI;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSpinner;
import javax.swing.JToolBar;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.InternationalFormatter;
import main.IqmTools;
import main.Tank;

public class IqmFracBoxGUI extends IqmOperatorJFrame implements IqmOperatorGUIBuilder,
															   ActionListener, ChangeListener {

	private  ParameterBlockJAI     pbJAI       = null;  //  @jve:decl-index=0:
	//public  ImgProcFuncJFrame    opGUIFrame  = null;
	//private  IqmFracBoxGUI       opGUIFrame  = null;  //actionPerformed events werden in dieser Klasse abgehandelt
 
	private int numBoxes = 0;
 	 
	private JPanel 			jPanelNumBoxes   = null;	
	private JLabel			jLabelNumBoxes	 = null;
	private JSpinner		jSpinnerNumBoxes = null;
	
	private JRadioButtonMenuItem  buttDBC            = null;	
	private JRadioButtonMenuItem  buttRDBC           = null;		
	private JRadioButtonMenuItem  buttGreyModeHidden = null;
	private JToolBar              jToolBarGreyMode   = null;
	private ButtonGroup           buttGroupGreyMode  = null;
	
	private JPanel 		jPanelRegression	= null;	
	private JLabel		jLabelRegression  	= null;;
	private JPanel 		jPanelRegStart   	= null;	
	private JLabel		jLabelRegStart	 	= null;
	private JSpinner	jSpinnerRegStart 	= null;	
	private JPanel 		jPanelRegEnd    	= null;	
	private JLabel		jLabelRegEnd		= null;
	private JSpinner	jSpinnerRegEnd  	= null;
	
	private  JCheckBox	jCheckBoxShowPlot			= null;
	private  JCheckBox	jCheckBoxDeleteExistingPlot = null;

	
	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pb
	 *
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pbJAI = arg;
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		
		pbJAI.setParameter("NumBoxes", ((Number)jSpinnerNumBoxes.getValue()).intValue());	
		
		if (buttDBC.isSelected())  pbJAI.setParameter("GreyMode", 0);
		if (buttRDBC.isSelected()) pbJAI.setParameter("GreyMode", 1);
	
		pbJAI.setParameter("RegStart", ((Number)jSpinnerRegStart.getValue()).intValue());
		pbJAI.setParameter("RegEnd",   ((Number)jSpinnerRegEnd.getValue()).intValue());

		if (jCheckBoxShowPlot.isSelected())  			pbJAI.setParameter("ShowPlot", 1);
		if (!jCheckBoxShowPlot.isSelected()) 			pbJAI.setParameter("ShowPlot", 0);
		if (jCheckBoxDeleteExistingPlot.isSelected()) 	pbJAI.setParameter("DeleteExistingPlot", 1);
		if (!jCheckBoxDeleteExistingPlot.isSelected())	pbJAI.setParameter("DeleteExistingPlot", 0);
		
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	
	/**
	 * This method sets the current parameter values
	 *
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmFracBoxGUI: Current image not defined");
	    	   return;
	       }
//	    int pixelSize = pi.getColorModel().getPixelSize();
//      int numBands  = pi.getNumBands(); 
//      String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
//      String type = IqmTools.getCurrImgTyp(pi);

		buttGreyModeHidden.setSelected(true);

		
	    jSpinnerNumBoxes.removeChangeListener(this);	
	    jSpinnerNumBoxes.setValue(pbJAI.getIntParameter("NumBoxes"));
	    jSpinnerNumBoxes.addChangeListener(this);
	    
		if (pbJAI.getIntParameter("GreyMode") == 0)  buttDBC.setSelected(true);
		if (pbJAI.getIntParameter("GreyMode") == 1)  buttRDBC.setSelected(true);

	    jSpinnerRegStart.removeChangeListener(this);	
	    jSpinnerRegStart.setValue(pbJAI.getIntParameter("RegStart"));
	    jSpinnerRegStart.addChangeListener(this);
	    
	    jSpinnerRegEnd.removeChangeListener(this);	
	    jSpinnerRegEnd.setValue(pbJAI.getIntParameter("RegEnd"));
	    jSpinnerRegEnd.addChangeListener(this);
	    
	    if (pbJAI.getIntParameter("ShowPlot") == 0)   jCheckBoxShowPlot.setSelected(false);
	    if (pbJAI.getIntParameter("ShowPlot") == 1)   jCheckBoxShowPlot.setSelected(true);
	    if (pbJAI.getIntParameter("DeleteExistingPlot") == 0)   jCheckBoxDeleteExistingPlot.setSelected(false);
	    if (pbJAI.getIntParameter("DeleteExistingPlot") == 1)   jCheckBoxDeleteExistingPlot.setSelected(true);
	}
	/**
	 * constructor
	 */
	public IqmFracBoxGUI() {

	}
	/**
	 * This method creates and shows the GUI
	 * 
	 */
	@Override
	public void createIqmOperatorGUI() {
		//opGUIFrame = new ImgProcFuncJFrame();  //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame = new IqmFracBoxGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//this.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("FracBox"); 
	
		PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
		int width      = pi.getWidth();
		int height     = pi.getHeight();
		numBoxes       = this.getMaxBoxNumber(width,  height);
		
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height+150);
		this.getJContentPane().add(getJPanelNumBoxes(),              getGridBagConstraintsNumBoxes());
		this.getJContentPane().add(getJToolBarGreyMode(),            getGridBagConstraintsGreyMode());
		this.getJContentPane().add(getJPanelRegression(),            getGridBagConstraintsRegression());
		this.getJContentPane().add(getJCheckBoxShowPlot(),		     getGridBagConstraintsShowPlot());
		this.getJContentPane().add(getJCheckBoxDeleteExistingPlot(), getGridBagConstraintsDeleteExistingPlot());	

		//this.repaint();
		if (pbJAI == null) pbJAI = new ParameterBlockJAI("IqmFracBox");  //nur wenn das erste mal aufgerufen
		//pbJAI = new ParameterBlockJAI("IqmFracBox"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pbJAI);
		
		setParameterValues();
		//Set image dependent initial values;
		
//		//System.out.println("IqmFracBoxGUI: numberMax: " + numberMax);
//		SpinnerModel sModel = new SpinnerNumberModel(numBoxes, 2, numBoxes, 1); //init, min, max, step
//		jSpinnerNumBoxes.removeChangeListener(this);
//		jSpinnerNumBoxes.setModel(sModel);
//	    jSpinnerNumBoxes.setValue(numBoxes);
//	    jSpinnerRegEnd.removeChangeListener(this);	
//	    jSpinnerRegEnd.setValue(numBoxes);
//	    jSpinnerRegEnd.addChangeListener(this);
//		JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNumBoxes.getEditor()  ;
//		JFormattedTextField ftf = defEditor.getTextField() ;
//		ftf.setEditable(false);
//		InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
//		DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
//		decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;	
//	    jSpinnerNumBoxes.addChangeListener(this);
//	
//	    setParameterBlock();
//		this.pack();
		//or short
		this.updateIqmOperatorGUI();
		this.pack();
	}
	//---------------------------------------------------------------------------------------------
	private int getMaxBoxNumber(int width, int height) { //inclusive original image
		float boxWidth =  1f;
		int number = 1; //inclusive original image
		while ((boxWidth <= width) || (boxWidth <= height)){
			boxWidth = boxWidth*2;
//			System.out.println("IqmFracBoxGUI: newBoxWidth: " +  newBoxWidth);
			number = number + 1;	
		}
		return number-1;
	}
	//---------------------------------------------------------------------------------------------
	private  GridBagConstraints getGridBagConstraintsNumBoxes(){
		GridBagConstraints gridBagConstraintsNumBoxes = new GridBagConstraints();
		gridBagConstraintsNumBoxes.gridx = 1;
		gridBagConstraintsNumBoxes.gridy = 5;
		gridBagConstraintsNumBoxes.gridwidth = 1;//?
		gridBagConstraintsNumBoxes.insets = new Insets(10,0,20,0);  //top left bottom right
		//gridBagConstraintsNumBoxes.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsNumBoxes;
	}
	private  GridBagConstraints getGridBagConstraintsGreyMode(){
		GridBagConstraints gridBagConstraintsGreyMode = new GridBagConstraints();
		gridBagConstraintsGreyMode.gridx = 2;
		gridBagConstraintsGreyMode.gridy = 5;
		gridBagConstraintsGreyMode.gridwidth = 2;//?
		gridBagConstraintsGreyMode.insets = new Insets(10,0,20,0);  //top left bottom right
		//gridBagConstraintsGreyMode.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsGreyMode;
	}
	private  GridBagConstraints getGridBagConstraintsRegression(){
		GridBagConstraints gridBagConstraintsRegression = new GridBagConstraints();
		gridBagConstraintsRegression.gridx = 1;
		gridBagConstraintsRegression.gridy = 6;
		gridBagConstraintsRegression.gridwidth = 3;//?
		gridBagConstraintsRegression.insets = new Insets(10,0,0,0);  //top left bottom right
		//gridBagConstraintsRegression.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsRegression;
	}
	private  GridBagConstraints getGridBagConstraintsShowPlot(){
		GridBagConstraints gridBagConstraintsShowPlot = new GridBagConstraints();
		gridBagConstraintsShowPlot.gridx = 1;
		gridBagConstraintsShowPlot.gridy = 7;
		gridBagConstraintsShowPlot.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsShowPlot.insets = new Insets(10,20,10,0); //top left bottom right
		//gridBagConstraintsShowPlot.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsShowPlot;
	}
	private  GridBagConstraints getGridBagConstraintsDeleteExistingPlot(){
		GridBagConstraints gridBagConstraintsDeleteExistingPlot = new GridBagConstraints();
		gridBagConstraintsDeleteExistingPlot.gridx = 2;
		gridBagConstraintsDeleteExistingPlot.gridy = 7;
		gridBagConstraintsDeleteExistingPlot.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsDeleteExistingPlot.insets = new Insets(10,0,10,0); //top left bottom right
		//gridBagConstraintsDeleteExistingPlot.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsDeleteExistingPlot;
	}

	//------------------------------------------------------------------------------------------------------

	/**
	 * This method updates the GUI if needed
	 * This method overrides IqmOperatorJFrame
	 */
	public void updateIqmOperatorGUI(){
		PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
	    if (pi == null){
	    	System.out.println("IqmFracBoxGUI: Current image not defined");
	    	return;
	    }
		int numBands = pi.getNumBands();
		int width    = pi.getWidth();
		int height   = pi.getHeight();
		numBoxes     = this.getMaxBoxNumber(width,  height);
	  	String type = IqmTools.getImgTyp(pi);
	    double typeGreyMax = IqmTools.getImgTypeGreyMax(pi);  	

	    // Set up the parameters for the Histogram object.
	    int[]    bins  = {(int)typeGreyMax+1, (int)typeGreyMax+1, (int)typeGreyMax+1}; // The number of bins  e.g. {256, 256, 256}
	    double[] lows  = {0.0D, 0.0D, 0.0D};                                           // The low incl.value  e.g. {0.0D, 0.0D, 0.0D}
	    double[] highs = {typeGreyMax+1, typeGreyMax+1, typeGreyMax+1};                // The high excl.value e.g. {256.0D, 256.0D, 256.0D}
	    // Create the parameter block.
	    ParameterBlock pbHisto = new ParameterBlock();
	    pbHisto.addSource(pi);   //Source image
	    pbHisto.add(null);       //ROI
	    pbHisto.add(1);          //Horizontal sampling
	    pbHisto.add(1);          //Vertical sampling
	    pbHisto.add(bins);       //Number of Bins
	    pbHisto.add(lows);       //Lowest inclusive values
	    pbHisto.add(highs);      //Highest exclusive values
	    // Perform the histogram operation.
		RenderedOp rOp = JAI.create("Histogram", pbHisto, null);
		// Retrieve the histogram data.
		Histogram histogram = (Histogram)rOp.getProperty("histogram");
		//System.out.println("IqmFracPyramidOperator: (int)typeGreyMax: " + (int)typeGreyMax);

		buttDBC.setEnabled(false);
		buttRDBC.setEnabled(false);
		
		for (int b = 0; b < numBands; b++) {
			double totalGrey   = (double)histogram.getSubTotal(b, 1, (int)typeGreyMax);  //without 0!
			double totalBinary = (double)histogram.getSubTotal(b, (int)typeGreyMax, (int)typeGreyMax);

			if (totalBinary != totalGrey){ // is not Binary
				buttDBC.setEnabled(true);
				buttRDBC.setEnabled(true);
			}
	
		}	
		
		//System.out.println("IqmFracBoxGUI: numberMax: " + numberMax);
		SpinnerModel sModel = new SpinnerNumberModel(numBoxes, 2, numBoxes, 1); //init, min, max, step
		jSpinnerNumBoxes.removeChangeListener(this);
		jSpinnerNumBoxes.setModel(sModel);
	    jSpinnerNumBoxes.setValue(numBoxes);
	    jSpinnerRegEnd.removeChangeListener(this);	
	    jSpinnerRegEnd.setValue(numBoxes);
	    jSpinnerRegEnd.addChangeListener(this);
		JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNumBoxes.getEditor()  ;
		JFormattedTextField ftf = defEditor.getTextField() ;
		ftf.setEditable(false);
		InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
		DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
		decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;	
	    jSpinnerNumBoxes.addChangeListener(this);
	
	    setParameterBlock();

	}
	//----------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jJPanelNumBoxes	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelNumBoxes() {
		if (jPanelNumBoxes == null) {
			jPanelNumBoxes = new JPanel();
			jPanelNumBoxes.setLayout(new BorderLayout());		
			jLabelNumBoxes = new JLabel("Box number: ");
			//jLabelNumBoxes.setPreferredSize(new Dimension(70, 22));
			jLabelNumBoxes.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(2, 2, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerNumBoxes = new JSpinner(sModel);
			//jSpinnerNumBoxes = new JSpinner();
			jSpinnerNumBoxes.setPreferredSize(new Dimension(60, 22));
			jSpinnerNumBoxes.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNumBoxes.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			ftf.setEditable(false);
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelNumBoxes.add(jLabelNumBoxes,   BorderLayout.NORTH);
			jPanelNumBoxes.add(jSpinnerNumBoxes, BorderLayout.CENTER);			
		}
		return jPanelNumBoxes;
	}
	//-------------------------------------------------------------------------------------------

	/**
	 * This method initializes the Option:
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtDBC() {
		//if (buttDBC == null) {
			buttDBC = new JRadioButtonMenuItem();
			buttDBC.setText("DBC");
			buttDBC.setToolTipText("Differential BC for grey scale images");
			buttDBC.addActionListener(this);
			buttDBC.setActionCommand("parameter");
		//}
		return buttDBC;
	}
	/**
	 * This method initializes the Option:
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtRDBC() {
		//if (buttRDBC == null) {
			buttRDBC = new JRadioButtonMenuItem();
			buttRDBC.setText("RDBC");
			buttRDBC.setToolTipText("Relative DBC for grey scale images");
			buttRDBC.addActionListener(this);
			buttRDBC.setActionCommand("parameter");
		//}
		return buttRDBC;
	}

	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtGreyModeHidden() {
		//if (buttGreyModeHidden == null) {
			buttGreyModeHidden = new JRadioButtonMenuItem();
			buttGreyModeHidden.setText("Hidden");
			buttGreyModeHidden.setVisible(false);
		//}
		return buttGreyModeHidden;
	}


	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarGreyMode() {
//		if (jToolBarGreyMode== null) {
			jToolBarGreyMode = new JToolBar("Options");
			jToolBarGreyMode.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarGreyMode.add(getJRadioButtonMenuButtDBC());
			jToolBarGreyMode.add(getJRadioButtonMenuButtRDBC());
			jToolBarGreyMode.add(getJRadioButtonMenuButtGreyModeHidden());
			//jToolBarGreyMode.addSeparator();
			this.setButtonGroupGreyMode();	//Gruppieren der JRadioButtons
//		}
		return jToolBarGreyMode;
	}

	private  void setButtonGroupGreyMode() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupGreyMode = new ButtonGroup();
			buttGroupGreyMode.add(buttDBC);
			buttGroupGreyMode.add(buttRDBC);
			buttGroupGreyMode.add(buttGreyModeHidden);
	}
	//-----------------------------------------------------------------------------------------

	/**
	 * This method initializes jJPanelRegression	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelRegression() {
		if (jPanelRegression== null) {
			jPanelRegression = new JPanel();
			jPanelRegression.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));				
			jPanelRegression.add(getJLabelRegression());
			jPanelRegression.add(getJPanelRegStart());
			jPanelRegression.add(getJPanelRegEnd());			
		}
		return jPanelRegression;
	}
	//------------------------------------------------------------------------------------------
	/**
	 * This method initializes jLabelRegression	
	 * 	
	 * @return javax.swing.JLabel
	 */
	private JLabel getJLabelRegression() {
		if (jLabelRegression == null){
			jLabelRegression = new JLabel("Regression:");
		}
		return jLabelRegression;	
	}
	//----------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jJPanelRegStart	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelRegStart() {
		if (jPanelRegStart == null) {
			jPanelRegStart = new JPanel();
			jPanelRegStart.setLayout(new FlowLayout());		
			jLabelRegStart = new JLabel("Start:");
			//jLabelRegStart.setPreferredSize(new Dimension(40, 22));
			jLabelRegStart.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(1, 1, numBoxes-1, 1); //init, min, max, step
			jSpinnerRegStart = new JSpinner(sModel);
			//jSpinnerRegStart = new JSpinner();
			jSpinnerRegStart.setPreferredSize(new Dimension(60, 22));
			jSpinnerRegStart.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerRegStart.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			ftf.setEditable(false);
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelRegStart.add(jLabelRegStart);
			jPanelRegStart.add(jSpinnerRegStart);			
		}
		return jPanelRegStart;
	}
	/**
	 * This method initializes jJPanelRegEnd	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelRegEnd() {
		if (jPanelRegEnd == null) {
			jPanelRegEnd = new JPanel();
			jPanelRegEnd.setLayout(new FlowLayout());		
			jLabelRegEnd = new JLabel("End:");
			//jLabelRegEnd.setPreferredSize(new Dimension(40, 22));
			jLabelRegEnd.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(numBoxes, 2, numBoxes, 1); //init, min, max, step
			jSpinnerRegEnd = new JSpinner(sModel);
			//jSpinnerRegEnd = new JSpinner();
			jSpinnerRegEnd.setPreferredSize(new Dimension(60, 22));
			jSpinnerRegEnd.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerRegEnd.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			ftf.setEditable(false);
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelRegEnd.add(jLabelRegEnd);
			jPanelRegEnd.add(jSpinnerRegEnd);			
		}
		return jPanelRegEnd;
	}
	//------------------------------------------------------------------------------------	
	/**
	 * This method initializes jCheckBoxShowPlot	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxShowPlot() {
		if (jCheckBoxShowPlot == null) {
			jCheckBoxShowPlot = new JCheckBox();
			jCheckBoxShowPlot.setText("ShowPlot");
			jCheckBoxShowPlot.addActionListener(this);			
			jCheckBoxShowPlot.setActionCommand("parameter");			
		}
		return jCheckBoxShowPlot;
	}
	//------------------------------------------------------------------------------------
	/**
	 * This method initializes jCheckBoxDeleteExistingPlot	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxDeleteExistingPlot() {
		if (jCheckBoxDeleteExistingPlot == null) {
			jCheckBoxDeleteExistingPlot = new JCheckBox();
			jCheckBoxDeleteExistingPlot.setText("DeleteExistingPlot");
			jCheckBoxDeleteExistingPlot.addActionListener(this);			
			jCheckBoxDeleteExistingPlot.setActionCommand("parameter");			
		}
		return jCheckBoxDeleteExistingPlot;
	}

    //----------------------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmFracBoxGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmFracBox".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {					
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}

		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	//-------------------------------------------------------------------------------------------------
	@Override
	public void stateChanged(ChangeEvent e) {
		
		numBoxes   = ((Number)jSpinnerNumBoxes.getValue()).intValue();
		int regStart = ((Number)jSpinnerRegStart.getValue()).intValue();
		int regEnd   = ((Number)jSpinnerRegEnd.getValue()).intValue();
		
		jSpinnerNumBoxes.removeChangeListener(this);
		jSpinnerRegStart.removeChangeListener(this);
		jSpinnerRegEnd.removeChangeListener(this);
		
		if (jSpinnerNumBoxes == e.getSource()) {
			//if(regEnd > numBoxes){
				jSpinnerRegEnd.setValue(numBoxes);
				regEnd = numBoxes; 
			//}
			if(regStart >= regEnd){
				jSpinnerRegStart.setValue(regEnd-1);
				regStart = regEnd-1;
			}
		}
		if (jSpinnerRegEnd == e.getSource()){
			if (regEnd > numBoxes){
				jSpinnerRegEnd.setValue(numBoxes);
				regEnd = numBoxes; 
			}
			if (regEnd <= regStart){
				jSpinnerRegEnd.setValue(regStart+1);
				regEnd = regStart+1; 
			}
		}
		if (jSpinnerRegStart == e.getSource()){
			if (regStart >= regEnd){
				jSpinnerRegStart.setValue(regEnd-1);
				regStart = regEnd-1;				
			}
		}
		
		jSpinnerNumBoxes.addChangeListener(this);
		jSpinnerRegStart.addChangeListener(this);
		jSpinnerRegEnd.addChangeListener(this);

		this.setParameterBlock();
		this.setParameterValues();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
		
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmFracBoxGUI gui = new IqmFracBoxGUI();
			    gui.createIqmOperatorGUI();

			}
		});
	}
	
}//END
